package com.jmex.model.ogredotscene;

import com.jme.scene.Skybox;
import com.jme.scene.state.LightState;
import com.jme.system.DisplaySystem;
import com.jmex.model.ogredotscene.loader.NodesLoader;
import com.jmex.model.ogredotscene.loader.ResourceLoader;
import com.jmex.model.ogredotscene.loader.TextureLoader;
import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import com.jmex.model.ogredotscene.gen.Scene;
import com.jmex.model.ogredotscene.loader.EnviromentLoader;
import com.jmex.model.ogredotscene.loader.ExternalsLoader;
import com.jmex.model.ogredotscene.loader.LightLoader;
import com.jmex.model.ogredotscene.loader.OgreLoader;

public class DotSceneHandler {

    public com.jme.scene.Node jmeSceneNode = new com.jme.scene.Node();
    public File baseDir;
    private LightState lightState = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
    private OgreLoader ogreLoader;

    private ResourceLoader resourceLoader;
    private TextureLoader textureLoader;
    private ExternalsLoader externals;
    private EnviromentLoader enviroment;
    private NodesLoader nodesloader;
    private LightLoader lightLoader;
    private com.jme.scene.Skybox jmeSkybox;


    public void loadScene(File dotSceneFile) {
        try {
            if (!dotSceneFile.isFile()) {
                throw new UnsupportedOperationException("argument is not a File");
            }
            
            lightState.detachAll();
            jmeSceneNode.setRenderState(lightState);
            jmeSceneNode.setName(dotSceneFile.getName());
            baseDir = dotSceneFile.getParentFile();
            JAXBContext jc = JAXBContext.newInstance("com.jmex.model.ogredotscene.gen");
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Scene dotScene = (Scene) unmarshaller.unmarshal(dotSceneFile);
            ogreLoader = new OgreLoader();

            if (dotScene.getResourceGroup() != null) {
                resourceLoader = new ResourceLoader();
                resourceLoader.registerResourceLocation(dotScene.getResourceGroup(), baseDir);

                textureLoader = new TextureLoader();
                textureLoader.loadbyResourceGroup(dotScene.getResourceGroup(), baseDir);
                textureLoader.loadbyExternals(dotScene.getExternals());
                ogreLoader.setMaterials(textureLoader.getMaterials());
            }

            if (dotScene.getExternals() != null) {
                externals = new ExternalsLoader();
                externals.load(dotScene.getExternals());
            }

            if (dotScene.getEnvironment() != null) {
                enviroment = new EnviromentLoader();
                enviroment.setLightState(lightState);
                enviroment.setMaterials(textureLoader.getMaterials());
                enviroment.setJmeScene(jmeSceneNode);
                enviroment.setEnviroment(dotScene.getEnvironment(), jmeSceneNode);
                if (enviroment.getJmeSkybox() != null){
                    jmeSkybox = enviroment.getJmeSkybox();
                }
            }

            if (dotScene.getNodes() != null) {
                nodesloader = new NodesLoader();
                nodesloader.setOgreLoader(ogreLoader);
                nodesloader.setLightState(lightState);
                nodesloader.buildNodes(dotScene.getNodes(), jmeSceneNode);
            }

            if (dotScene.getLight() != null){
                lightLoader = new LightLoader();
                lightLoader.setLightState(lightState);
                lightLoader.setLights(dotScene.getLight());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public com.jme.scene.Node getJmeSceneNode() {
        return jmeSceneNode;
    }

    public void setJmeSceneNode(com.jme.scene.Node jmeSceneNode) {
        this.jmeSceneNode = jmeSceneNode;
    }

    public Skybox getJmeSkybox() {
        return jmeSkybox;
    }

    public void setJmeSkybox(Skybox jmeSkybox) {
        this.jmeSkybox = jmeSkybox;
    }


}
